
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module data
* @author: Hjava
* @description: 透传模块
* @since: 2018-07-22 14:38:16
*/
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var data_constant_1 = __webpack_require__(/*! _data/data.constant */ "./src/core/data/data.constant.ts");
var data_session_1 = __webpack_require__(/*! _data/data.session */ "./src/core/data/data.session.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var DataSDK = /** @class */ (function () {
    function DataSDK(loginSDK) {
        this._loginSDK = null;
        this.constant = data_constant_1.default;
        this._loginSDK = loginSDK;
        eventEmitter_1.default.on(data_constant_1.default.DATA.URI_DATA_SEND_REQ, this._onmessage.bind(this));
        // 透传的短连离线接口会触发这个回调
        eventEmitter_1.default.on(constant_1.MESSAGE_EVENT.D_MESSAGE_OFFLINE, this.sendAck.bind(this));
    }
    /**
     * 透传消息封装接口，只能用于发送数据类型是string的透传消息
     *
     * @param {*} {type = 0, data = '', channelId = 0}
     * @memberof DataSDK
     */
    DataSDK.prototype.sendDataMessage = function (_a) {
        var _b = _a.type, type = _b === void 0 ? 0 : _b, _c = _a.data, data = _c === void 0 ? '' : _c, _d = _a.channelId, channelId = _d === void 0 ? 0 : _d, _e = _a.fromUid, fromUid = _e === void 0 ? this._loginSDK.getUID() : _e;
        log_1.default.log('DataSDK::sendDataMessage type:' + type + ' data:' + data + ' channelId:' + channelId + ' fromUid:' + fromUid);
        var session = new data_session_1.default.DataSendClientReq({
            type: type,
            data: data,
            fromUid: fromUid,
            channel: channelId
        });
        this.send(session);
    };
    DataSDK.prototype.send = function (message) {
        this._loginSDK._sendTransUp(data_constant_1.default.SVID_DATA, message);
    };
    /**
     * @param message {!Message}
     * @description 接受透传消息
     */
    DataSDK.prototype._onmessage = function (message) {
        log_1.default.log('DataSDK::_onmessage receive online data message msgId:' + (message ? message.msgId : ''));
        this.sendAck([message]);
        //可以通过message.category == 'DATA' 字段判断是否是透传包
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.MESSAGE, message);
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.D_MESSAGE, message);
    };
    DataSDK.prototype.sendAck = function (messages) {
        var _this = this;
        var typeMidMap = {};
        messages.forEach(function (element) {
            var type = element.type;
            if (typeMidMap[type]) {
                typeMidMap[type].push(element.msgId);
            }
            else {
                typeMidMap[type] = [element.msgId];
            }
        });
        Object.keys(typeMidMap).forEach(function (key) {
            var mids = typeMidMap[key];
            if (mids && mids.length > 0) {
                _this.send(new data_session_1.default.DataSendAck({
                    uid: _this._loginSDK.getUID(),
                    type: key,
                    messageIds: mids
                }));
            }
        });
    };
    DataSDK.Session = data_session_1.default;
    DataSDK.constant = data_constant_1.default;
    return DataSDK;
}());
exports.default = DataSDK;


//# sourceURL=webpack://MTDX/./src/core/data/index.ts?